package com.timmy.google_play.fragment;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.timmy.google_play.base.BaseFragment;
import com.timmy.google_play.base.LoadingPager;
import com.timmy.google_play.protocol.HotProtocol;
import com.timmy.google_play.utils.UIUtils;
import com.timmy.google_play.views.FlowLayout;

import java.util.List;
import java.util.Random;

/**
 * Created by Timmy on 2016/12/7.
 */
public class HotFragment extends BaseFragment {

    private List<String> mDatas;

    @Override
    protected LoadingPager.LoadedResult initData() {

        HotProtocol protocol = new HotProtocol();

        try {
            mDatas = protocol.loadData(0);

            return checkResData(mDatas);
        } catch (Exception e) {
            e.printStackTrace();

            return LoadingPager.LoadedResult.ERROR;
        }
    }

    @Override
    protected View initSuccessView() {

        ScrollView scrollView = new ScrollView(UIUtils.getContext());
        FlowLayout flowLayout = new FlowLayout(UIUtils.getContext());

        for (final String data : mDatas) {

            TextView textView = new TextView(UIUtils.getContext());

            textView.setText(data);

            //加入容器
            flowLayout.addView(textView);

            //处理tv样式
            textView.setTextColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER);

            int width  = UIUtils.dp2px(3);
            int height = UIUtils.dp2px(5);
            textView.setPadding(width, height, width, height);         //左上右下

            //默认时候圆角的随机颜色背景
            GradientDrawable normalBg = new GradientDrawable();
            Random           random   = new Random();

            int alpha = 255;
            int blue  = random.nextInt(150) + 50;
            int red   = random.nextInt(150) + 50;
            int green = random.nextInt(150) + 50;
            int argb  = Color.argb(alpha, red, green, blue);
            normalBg.setColor(argb);
            normalBg.setCornerRadius(UIUtils.dp2px(5));

            //设置按下去时候的背景图片
            GradientDrawable pressedBg = new GradientDrawable();
            pressedBg.setColor(Color.DKGRAY);
            pressedBg.setCornerRadius(UIUtils.dp2px(5));


            StateListDrawable bgSelector = new StateListDrawable();

            bgSelector.addState(new int[]{android.R.attr.state_pressed}, pressedBg);
            bgSelector.addState(new int[]{}, normalBg);

            textView.setBackgroundDrawable(bgSelector);

            textView.setClickable(true);


            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(UIUtils.getContext(), data, Toast.LENGTH_SHORT).show();
                }
            });
        }

        scrollView.addView(flowLayout);

        return scrollView;
    }
}
